package org.keqinglee.study.concurrent;

import java.lang.ref.WeakReference;
import java.util.concurrent.TimeUnit;

/**
 * @description: org.keqinglee.study.concurrent
 * @author: liqing
 * @date: 2022/9/2
 */
public class ThreadLocalTest implements Runnable {

    private static ThreadLocal<String> threadLocal1 = ThreadLocal.withInitial(() -> "threadLocal1");
    private static ThreadLocal<String> threadLocal2 = ThreadLocal.withInitial(() -> "threadLocal2");

    public static void main(String[] args) {
        //黄金分割比
//        int hashIncrement = 0x61c88647;
//        System.out.println(hashIncrement);
//        System.out.println(Integer.MAX_VALUE);

        //test1();

//        ThreadLocalTest runnable = new ThreadLocalTest();
//        for (int i = 0; i < 2; i++) {
//            Thread thread = new Thread(runnable);
//            thread.start();
//        }
//
//        try {
//            TimeUnit.SECONDS.sleep(200);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//        System.out.println("运行完毕");

    }

    @Override
    public void run() {
        threadLocal1.set("threadLocal1111");
        threadLocal2.set("threadLocal2222");
    }

    public static void test1() {
        for (int i = 0; i < 2; i++) {
            String threadName = String.format("【测试线程%d】", i);
            new Thread(() -> {
                System.out.println(threadName + "val=" + threadLocal1.get());
                threadLocal1.set("threadLocal1111");
                System.out.println(threadName + "val=" + threadLocal1.get());
            }, threadName).start();
        }

        try {
            TimeUnit.SECONDS.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("运行完毕");
    }


}
